package com.sihan.framework.etm.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.context.ApplicationContext;

public class BizErrorHelper {

	/**
	 * 
	 * @param springContext 
	 * @param masterErrorKey 主错误key
	 * @param tranErrorKey  详细错误key
	 * @param tags  错误关联资源
	 * @return
	 * @description TODO
	 * @return BizErrorModel
	 * @author zx
	 * @date 2015年6月4日 上午11:13:26
	 */
	public static BizErrorModel getBizErrorModel(ApplicationContext springContext, String masterErrorKey,String tranErrorKey) {
		BizErrorModel bizError = new BizErrorModel();
		String masterError = springContext.getMessage(masterErrorKey, null,
				Locale.getDefault());
		bizError.setReason(masterError);
		List<BizErrorItemModel> errors = new ArrayList<BizErrorItemModel>();
		BizErrorItemModel bizErrorItemModel = new BizErrorItemModel();
		bizErrorItemModel.setErrorCode(tranErrorKey);
		bizErrorItemModel.setReason(springContext.getMessage(masterErrorKey+ "_" + tranErrorKey,
				null, Locale.getDefault()));
		errors.add(bizErrorItemModel);
		bizError.setErrors(errors);
		return bizError;
	}
	
	/**
	 * 获取错误模型，为错误提示增加了参数化
	 * @param springContext applicationContext 
	 * @param masterErrorKey
	 * @param tranErrorKey
	 * @param args Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
	 * @return
	 * @date 2016年8月17日 下午4:06:49
	 * @author ZyL
	 */
	public static BizErrorModel getBizErrorModelArguments(ApplicationContext springContext, String masterErrorKey, String tranErrorKey, Object... args){
		BizErrorModel bizError = new BizErrorModel();
		String masterError = springContext.getMessage(masterErrorKey, null,
				Locale.getDefault());
		bizError.setReason(masterError);
		List<BizErrorItemModel> errors = new ArrayList<BizErrorItemModel>();
		BizErrorItemModel bizErrorItemModel = new BizErrorItemModel();
		bizErrorItemModel.setErrorCode(tranErrorKey);
		bizErrorItemModel.setReason(springContext.getMessage(masterErrorKey+ "_" + tranErrorKey,
				args, Locale.getDefault()));
		errors.add(bizErrorItemModel);
		bizError.setErrors(errors);
		return bizError;
	}
	
}
